//
//  LTChooseContentViewController.m
//  LearnTeacher
//
//  Created by 高刘通 on 2024/6/7.
//

#import "LTChooseContentViewController.h"
#import "LTChooseContentView.h"
#import "LTChooseContentLeftModel.h"
#import "LTChooseContentModel.h"
#import "LTChooseUnitViewController.h"
#import "LTHomeworkAipManager.h"
#import "LTChooseContentLeftModel.h"
#import "NSObject+YYModel.h"
#import "LTChooseUnitModel.h"

@interface LTChooseContentViewController ()
@property(strong, nonatomic) LTChooseContentView *rootView;
@end

@implementation LTChooseContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.contentView addSubview:self.rootView];
    self.titleLabel.text = @"已选择内容";
    [self getData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unitChanged:) name:ChooseUnitSuccess object:nil];
}

- (void)unitChanged:(NSNotification *)notif {
    [self dismiss];
}

- (void)getData {
    __weak typeof(self) weakSelf = self;
    [LTHomeworkAipManager getTextBookWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
        id data = dic[@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray *dataArray = (NSArray *)data;
            NSMutableArray *models = @[].mutableCopy;
            for (NSDictionary *dict in dataArray) {
                LTChooseContentLeftModel *model = [LTChooseContentLeftModel modelWithJSON:dict];
                [models addObject:model];
            }
            if (models.count == 0) {
                [SVProgressHUD showImage:nil status:@"暂无数据~"];
                [weakSelf dismiss];
            }
            weakSelf.rootView.leftDatasource = models;
        }
    }];
}

- (void)getUnitData:(NSString *)identifier name:(NSString *)name {
    __weak typeof(self) weakSelf = self;
    [SVProgressHUD show];
    [LTHomeworkAipManager getTextBookUnitWithParam:@{@"id":identifier} result:^(NSDictionary * _Nonnull dic) {
        id data = dic[@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray *dataArray = (NSArray *)data;
            NSMutableArray *models = @[].mutableCopy;
            for (NSDictionary *dict in dataArray) {
                LTChooseUnitModel *model = [LTChooseUnitModel modelWithJSON:dict];
                [models addObject:model];
            }
            if (models.count == 0) {
                [SVProgressHUD showImage:nil status:@"暂无数据~"];
            }else {
                [SVProgressHUD dismiss];
                LTChooseUnitViewController *contentVC = [[LTChooseUnitViewController alloc] init];
                contentVC.dataSource = models;
                contentVC.name = name;
                contentVC.versionId = identifier;
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:contentVC];
                navc.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakSelf.navigationController presentViewController:navc animated:NO completion:^{
                    
                }];
            }
        }
    }];
}

- (void)switchtextbooksp:(NSString *)versionId name:(NSString *)name {
//    __weak typeof(self) weakSelf = self;
//    [SVProgressHUD show];
//    [LTHomeworkAipManager switchtextbookspWithParam:@{@"versionId":versionId} result:^(NSDictionary * _Nonnull dic) {
//        [SVProgressHUD dismiss];
//        [weakSelf getUnitData:versionId name:name];
//    }];
    [self getUnitData:versionId name:name];
}

- (LTChooseContentView *)rootView {
    if (!_rootView) {
        _rootView = [[LTChooseContentView alloc] initWithFrame:self.contentView.bounds];
        __weak typeof(self) weakSelf = self;
        [_rootView setDidSelectHandler:^(LTChooseContentModel *model) {
            [weakSelf switchtextbooksp:model.versionId name:model.book_name];
        }];
    }
    return _rootView;
}

- (void)dealloc {
    NSLog(@"dealloc");
}
@end
